<template>
  <div class="search-index container">
    <Header></Header>
    <section>
      <div class="search-history" v-if="searchArr.length">
        <h2>
          <i class="iconfont icon-shijian"></i>
          <span>历史记录</span>
          <span @click="clearArr">清空历史记录</span>
        </h2>
        <ul>
          <li
            v-for="(item, index) in searchArr"
            :key="index"
            @click="goSearchList(item)"
          >
            {{ item }}
          </li>
        </ul>
      </div>
      <div v-else>
        <van-empty image="search" description="暂无搜索..." />
      </div>
      <div>
        <h2>
          <i class="iconfont icon-huore"></i>
          <span>热门搜索</span>
        </h2>
      </div>
      <div>
        <h2>
          <i class="iconfont icon-dianzan"></i>
          <span>推荐商品</span>
        </h2>
        <Like></Like>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Header from "@/components/search/Header.vue";
import Like from "@/components/home/Like.vue";
import Tabbar from "@/components/common/Tabbar.vue";

import { MessageBox } from "mint-ui";
export default {
  components: {
    Like,
    Tabbar,
    Header,
  },
  data() {
    return {
      searchArr: [],
    };
  },
  created() {
    this.searchArr = JSON.parse(localStorage.getItem("searchList")) || [];
  },
  methods: {
    clearArr() {
      MessageBox({
        title: "提示",
        message: "确定执行此操作?",
        showCancelButton: true,
      }).then((res) => {
        if (res == "confirm") {
          //删除本地存储
          localStorage.removeItem("searchList");
          //清空数据
          this.searchArr = [];
        }
      });
    },

    //点击历史搜索，进入搜索结果页面
    goSearchList(item) {
      this.$router.push({
          name:'search-list',
          query:{
            key:item
          }
      })
    },
  },
};
</script>

<style lang="scss" scoped>

section {
  background-color: #f5f5f5;
}
.search-history h2 {
  position: relative;
  padding: 0.2857rem;
  font-size: 0.5143rem;
  font-weight: 500;
}
.search-history h2 i {
  padding-right: 0.1429rem;
  color: red;
  font-size: 0.5143rem;
  font-weight: 700;
}
.search-history h2 span:last-child {
  position: absolute;
  right: 0.5714rem;
}
.search-history ul {
  display: flex;
  align-content: center;
  flex-wrap: wrap;
  padding: 0.2857rem 0;
}
.search-history ul li {
  margin-left: 0.2857rem;
  padding: 0.0857rem 0.1714rem;
  font-size: 0.4rem;
  color: rgb(168, 168, 168);
  border: 1px solid #ccc;
}
</style>
